<?php
/*
 * Copyright (C) 2014 Peslook
 *                    Vietnam
 *                    http://www.peslook.com
 *                    support@peslook.com
 *
 * Authors:
 *  Loi Nguyen <loint@peslook.com>
 *
 * This file is part of the peslook project
 */
namespace App\Controller;

use App\Interfaces\Controller\AppControllerInterface;
use App\Library\System\Model\AjaxModel;
use App\Library\System\Model\AppModel;
use App\Library\System\Model\CountryModel;
use App\Library\System\Model\LanguageModel;
use App\Library\System\Model\UserModel;
use App\Library\System\Sub\Auth;
use App\Library\System\Sub\Country;

/**
 * App Controller
 *
 * @category   App
 * @package    App\Controller
 * @author     Loi Nguyen <loint@peslook.com>
 * @copyright  Peslook
 * @version    1.0
 * @since      Class available since Release 1.0
 */
class AppController extends BaseController implements AppControllerInterface
{
    /**
     * JavaScript Variable
     *
     * @var array
     */
    public $js;

    /**
     * Constructor
     *
     */
    public function __construct()
    {
        // Custom css and js for every page
        $css = array();
        //$css = array('app_header', 'app_footer', 'app_languages', 'app_confirm_register', 'welcome_login_form');
        //$css = array('app');
        //$js = array('jquery.ui', 'app', 'social', 'language', 'dragdrop', 'demo');
        $auth = new Auth();
        if ($auth->login) {
            $coffee = array('app', 'language');
            $less = array('index_header', 'app_footer', 'app_languages','app_box_languages','app_login_fail', 'welcome_login_form');
        } else {
            $coffee = array('app', 'language');
            $less = array('app_header','app_header', 'app_footer', 'app_languages','app_box_languages','app_login_fail', 'welcome_login_form');
        }


        // Set layout folder attach css and js file
        $this->setLayout('app', $less, $coffee);

        $this->auth = new Auth();
        $this->app = AppModel::getInstance();
    }

    /**
     * Index action
     * @return array|void
     */
    public function indexAction()
    {
        return $this->error(404);
    }

    /**
     * Login action
     *
     * @return \Zend\View\Model\ViewModel
     */
    public function loginAction()
    {
        if ($this->auth->login) {
            $this->go('/');
        } else {
            // Get login information from $_POST
            $username = $this->post('app_login_username');
            $password = $this->post('app_login_hash');
            $remember = $this->post('app_login_remember') ? true : false;

            if (isset($username) && ($this->auth->login($username, $password, $remember))) {
                if ($this->app->checkActive($username) == true) {
                    // Go back
                    $this->back();
                }
                else {
                    $this->registerAction();
                }
            } else {

                $page = $this->createPage('login');

                $this->out->title = 'Login';
                $page->login = true;

                $this->support($page);

                return $page;
            }
        }
    }

    /**
     * Logout action
     *
     * @return mixed|void
     */
    public function logoutAction()
    {
        $this->auth->logout();
        $this->go('/');
    }

    /**
     * Register action
     *
     * @router /app/register
     * @return mixed|\Zend\View\Model\ViewModel
     */
    public function registerAction()
    {
        $finish = $this->post('finish');

        if (!is_null($finish)) {
            $question = $this->post('question');
            $answer = $this->post('answer');
            $country = $this->post('country');
            $language = $this->post('language');

            $this->debug($question);

            $this->app->saveInformation($question, $answer, $language, $country);
            $this->go('/app/introduce');
        } else {
            //$this->debug('register');
            $coffee = array('app', 'language', 'lib/bootstrap-combobox');
            $less = array('app_header', 'app_footer', 'app_steps', 'app_languages', 'app_confirm_register', 'welcome_login_form');
            $this->setLayout('app', $less, $coffee);
            $page = $this->createPage('register');
            $this->out->title = 'Register';

            $name = $this->post('app_register_fullname');
            $email = $this->post('app_register_email');
            $hash = $this->post('app_register_hash');
            $this->support($page);

            $country = new CountryModel();
            $this->js('app.countries', $this->json($country->getAllCountries(), false), false, false);
            $this->app->saveRegisterForm($name, $email, $hash);
            //$this->app->createAccount();

            return $page;
        }
    }

    /**
     * Forgot action
     *
     * @router app/forgot
     * @return mixed|\Zend\View\Model\ViewModel
     */
    public function forgotAction()
    {
        $this->less = array('app_header', 'app_footer', 'app_languages', 'welcome_login_form');
        $page = $this->createPage('forgot');
        $this->out->title = 'Forgot your password ?';

        $this->support($page);

        return $page;
    }

    // ABOUT PAGE
    public function aboutAction()
    {
        $page = $this->createPage('about');
        $this->out->title = 'About';


        $this->support($page);

        return $page;
    }

    // DEVELOPERS PAGE
    public function developersAction()
    {
        $page = $this->createPage('developers');
        $this->out->title = 'Developers';

        $this->support($page);

        return $page;
    }

    // CAREERS PAGE
    public function careersAction()
    {
        $page = $this->createPage('careers');
        $this->out->title = 'Careers';


        $this->support($page);

        return $page;
    }

    // VOLUNTEERS PAGE
    public function volunteersAction()
    {
        $page = $this->createPage('volunteers');
        $this->out->title = 'Volunteers';

        $this->support($page);

        return $page;
    }

    public function settingAction()
    {
        $page = $this->createPage('setting');
        $this->out->title = 'Setting';


        $this->support($page);

        return $page;
    }

    public function pageAction()
    {
        $page = $this->createPage('page');
        $this->out->title = 'Page';

        $this->support($page);

        return $page;
    }

    public function tuiAction()
    {
        $page = $this->createPage('tui');
        $this->out->title = 'Tui';

        $this->support($page);

        return $page;
    }

    public function introduceAction()
    {
        if (!$this->auth->login) {
            return $this->go('/app/about');
        }

        $controller_class = __NAMESPACE__.'\\'.'Index';

        return $this->forward()->dispatch($controller_class, array(
            'action' => 'index',
            'id' => $this->auth->currentUser(),
            'introduce' => true
        ));
    }

}
